package com.zscat.mallplus.qsg.contrller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgRecharge;
import com.zscat.mallplus.qsg.QsgRoom;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.qsg.service.IRechargeService;
import com.zscat.mallplus.qsg.service.IRoomService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@RestController
@Api(tags = "RoomController", description = "房间管理")
@RequestMapping("/admin/qsg/room")
public class RoomController {

    @Resource
    private IRoomService roomService;
    @Resource
    private IDeviceService deviceService;

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有房间列表")
    @ApiOperation("根据条件查询所有房间列表")
    @GetMapping(value = "/list")
    public Object getRoomByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id", UserUtils.getCurrentMember().getQsgStoreId());
        try {
            IPage<QsgRoom> roomIPage = roomService.page(new Page<>(pageNum, pageSize), qw);
            return new CommonResult().success(roomIPage);
        } catch (Exception e) {
            log.error("根据条件查询所有房间列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "qsg", REMARK = "保存房间表")
    @ApiOperation("保存房间表")
    @PostMapping(value = "/create")
    public Object saveRoom(@RequestBody QsgRoom entity) {
        try {
            entity.setStoreId(UserUtils.getCurrentMember().getQsgStoreId().intValue());
            entity.setCreateDatetime(new Date());
            Long deviceId = entity.getDeviceId();
            QsgDevice device = deviceService.getById(deviceId);
            entity.setDeviceCode(device.getDeviceCode());
            if (roomService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存房间表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "更新房间表")
    @ApiOperation("更新房间表")
    @PostMapping(value = "/update/{id}")
    public Object updateRoom(@RequestBody QsgRoom entity) {
        try {
            QsgRoom room = roomService.getById(entity.getId());
            Long deviceId = room.getDeviceId();
            QsgDevice device = deviceService.getById(deviceId);
            room.setDeviceCode(device.getDeviceCode());
            room.setStatus(entity.getStatus());
            room.setBeforeStatus(room.getStatus());
            if (roomService.updateById(room)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新房间表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "删除房间表")
    @ApiOperation("删除房间表")
    @GetMapping(value = "/delete/{id}")
    public Object deleteRoom(@ApiParam("房间表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("房间表id");
            }
            if (roomService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除房间表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "给房间表分配房间表")
    @ApiOperation("查询房间表明细")
    @GetMapping(value = "/{id}")
    public Object getRoomById(@ApiParam("房间表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("房间表id");
            }
            QsgRoom room = roomService.getById(id);
            return new CommonResult().success(room);
        } catch (Exception e) {
            log.error("查询房间表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }
}
